﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL_Class
{
    public class SachDT
    {
        NLLDataContext db;
        public string AddNewEbook(Ebook ent)
        {
            db = new NLLDataContext();
            string result = string.Empty;
            db.Ebooks.InsertOnSubmit(ent);
            try
            {
                db.SubmitChanges();
                result = "Thêm mới thành công";
            }
            catch (Exception ex)
            {
                throw ex;  
            }
            return result;
        }

        public void UpdateEbook(Ebook ent)
        {
            db = new NLLDataContext();

            Ebook e = db.Ebooks.Single(p => p.ID == ent.ID);

            e.Title = ent.Title;
            e.ImageUrl = ent.ImageUrl;
            e.Description = ent.Description;
            e.FileUrl = ent.FileUrl;
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {    
                throw ex;
            }
        }

        public void DeleteEbook(int ID)
        {
            db = new NLLDataContext();
            var ebook = from p in db.Ebooks
                         where p.ID.Equals(ID)
                         select p;

            db.Ebooks.DeleteAllOnSubmit(ebook);
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
